package bank;

import java.util.*;

import framework.account.IAccount;
import framework.customer.ICustomer;

/**
 * @author ekamoliddinov
 */
public class Model {
    private List<Map<String, String>> tableRows;
    private Set<ICustomer> customers;
    private static Model instance;

    private Model() {
        tableRows = new ArrayList<Map<String, String>>();
        customers = new HashSet<ICustomer>();
    }

    public static Model getInstance() {
        if (instance == null)
            instance = new Model();
        return instance;
    }

    public boolean isExist(String name) {
        for (ICustomer customer : customers) {
            if (customer.getName().equals(name))
                return true;
        }
        return false;
    }

    public ICustomer getCustomerByName(String name) {
        for (ICustomer customer : customers) {
            if (customer.getName().equals(name))
                return customer;
        }
        return null;
    }

    public IAccount getAccount(String number) {
        for (ICustomer customer : customers) {
            IAccount account = customer.getAccount(number);
            if (account != null)
                return account;
        }
        return null;
    }

    public void addCustomer(ICustomer customer) {
        customers.add(customer);
    }

    public void addRow(Map<String, String> map) {
        tableRows.add(map);
    }

    public IAccount getSelectedAccount(Integer selectedIndex) {
        if (tableRows.size() < selectedIndex - 1)
            return null;
        String number = tableRows.get(selectedIndex).get("ccNumber");
        return getAccount(number);
    }

    public void refreshRows() {
        tableRows.clear();
        for (ICustomer customer : customers) {
            for (IAccount account : customer.getAaccounts()) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("type", account.getType());
                map.put("name", customer.getName());
                map.put("street", customer.getStreet());
                map.put("city", customer.getCity());
                map.put("zip", customer.getZip());
                map.put("email", customer.getEmail());
                map.put("state", customer.getState());
                map.put("ccNumber", account.getAccountNo());
                map.put("amount", String.valueOf(account.getBalance()));
                tableRows.add(map);
            }
        }
    }

    public List<IAccount> getAccounts() {
        List<IAccount> list = new ArrayList<IAccount>();
        for (ICustomer customer : customers) {
            for (IAccount account : customer.getAaccounts()) {
                list.add(account);
            }
        }
        return list;
    }

    public List<Vector<String>> getRows() {
        List<Vector<String>> list = new ArrayList<Vector<String>>();
        for (Map<String, String> cusMap : tableRows) {
            Vector<String> row = new Vector<String>();
            row.add(cusMap.get("name"));
            row.add(cusMap.get("ccNumber"));
            row.add(cusMap.get("expDate"));
            row.add(cusMap.get("type"));
            row.add(cusMap.get("balance"));
        }
        return list;
    }

    public Map<String, String> getAccountData(String number) {
        for (Map<String, String> cusMap : tableRows) {
            if(cusMap.get("ccNumber").equals(number))
                return cusMap;
        }
        return null;
    }
}